package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBuildUnit;
import com.ruoyi.system.service.ISysBuildUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单元管理Controller
 * 
 * @author ruoyi
 * @date 2021-01-28
 */
@RestController
@RequestMapping("/system/unit")
public class SysBuildUnitController extends BaseController
{
    @Autowired
    private ISysBuildUnitService sysBuildUnitService;

    /**
     * 查询单元管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBuildUnit sysBuildUnit)
    {
        startPage();
        List<SysBuildUnit> list = sysBuildUnitService.selectSysBuildUnitList(sysBuildUnit);
        return getDataTable(list);
    }

    /**
     * 获取单元下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysBuildUnit sysBuildUnit)
    {
        List<SysBuildUnit> units = sysBuildUnitService.selectSysBuildUnitList(sysBuildUnit);
        //这里就把根据条件查到的部门信息构建成为部门树传回展示
        return AjaxResult.success(sysBuildUnitService.buildUnitTreeSelect(units));
    }

    /**
     * 导出单元管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:export')")
    @Log(title = "单元管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysBuildUnit sysBuildUnit)
    {
        List<SysBuildUnit> list = sysBuildUnitService.selectSysBuildUnitList(sysBuildUnit);
        ExcelUtil<SysBuildUnit> util = new ExcelUtil<SysBuildUnit>(SysBuildUnit.class);
        return util.exportExcel(list, "unit");
    }

    /**
     * 获取单元管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysBuildUnitService.selectSysBuildUnitById(id));
    }

    /**
     * 新增单元管理
     */
    @PreAuthorize("@ss.hasPermi('system:unit:add')")
    @Log(title = "单元管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBuildUnit sysBuildUnit)
    {
        return toAjax(sysBuildUnitService.insertSysBuildUnit(sysBuildUnit));
    }

    /**
     * 修改单元管理
     */
    @PreAuthorize("@ss.hasPermi('system:unit:edit')")
    @Log(title = "单元管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBuildUnit sysBuildUnit)
    {
        return toAjax(sysBuildUnitService.updateSysBuildUnit(sysBuildUnit));
    }

    /**
     * 删除单元管理
     */
    @PreAuthorize("@ss.hasPermi('system:unit:remove')")
    @Log(title = "单元管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysBuildUnitService.deleteSysBuildUnitByIds(ids));
    }
}
